
function showCustomAlert(message, duration) {
    let customAlert = document.getElementById('customAlert');
    if (!customAlert) {
        div_outter=document.createElement('div');
        customAlert = document.createElement('div');
        customAlert.id = 'customAlert';
        // customAlert.style.margin='7px 12px';
        const alertMessage = document.createElement('p');
        alertMessage.id = 'alertMessage';
        customAlert.appendChild(alertMessage);

        document.body.appendChild(customAlert);
    }

    const alertMessage = document.getElementById('alertMessage');

    alertMessage.textContent = message;
    if(alertMessage.textContent==null){
        customAlert.style.display = 'none';
    }

    customAlert.style.display = 'block';
    setTimeout(() => {
        customAlert.style.display = 'none';
    }, duration);
}
// let follow_button=document.getElementsByClassName('follow-button')[0]
follow_button.addEventListener('click', function() {
    let userId = this.value;
    console.log(userId);
    // const userIdNum = Number(value);
    if (this.classList.contains('following')) {
        // 当前是已关注状态，执行取消关注逻辑
        $.ajax({
            url: `/cancelFollow?id=${userId}`,
            type: 'POST',
            success: function(result) {
                showCustomAlert('取消关注成功', 3000);
                follow_button.classList.remove('following');
                follow_button.textContent = '关注';
            },
            error: function(error) {
                showCustomAlert('取消关注失败: ' + error.statusText, 3000);
            }
        });
    }
    else {
        // 当前是未关注状态，执行关注逻辑
        $.ajax({
            url: `/followOthers?id=${userId}`,
            type: 'GET',
            success: function(result) {
                showCustomAlert('关注成功', 3000);
                follow_button.classList.add('following');
                follow_button.textContent = '取消关注';
            },
            error: function(error) {
                showCustomAlert('关注失败: ' + error.statusText, 3000);
            }
        });
    }
})


